﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;

public partial class ThiNghiemTram : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    NpoiExport cl = new NpoiExport();
    string frmName = "frmThiNghiemTram";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static int PageNumber = 10;
    static int CurrentPage = 1;
    static DataTable dt, dtPrint;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {
                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;


                    Title = "Thí nghiệm tại trạm| " + Header;
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThangLoc.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThangLoc.SelectedValue = DateTime.Now.Month.ToString();

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNamLoc.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNamLoc.SelectedValue = year2.ToString();


                    btnCancel_Click(sender, e);

                    LoadPhuGia(dlLoaiPhuGia);
                    LoadMac(dlMac, "--Chọn mác--");
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                p = new SqlParameter[19];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtNgayThang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@Mac", ex.GetGuid(dlMac.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@LoaiDa", int.Parse(dlLoaiDa.SelectedValue));
                p[3].SqlDbType = SqlDbType.Int;
                p[4] = new SqlParameter("@LoaiPhuGia", ex.GetGuid(dlLoaiPhuGia.SelectedValue));
                p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                p[5] = new SqlParameter("@DoSutTK", txtDoSutTK.Text.Trim());
                p[5].SqlDbType = SqlDbType.VarChar;
                p[6] = new SqlParameter("@DoSutTT", txtDoSutTT.Text.Trim());
                p[6].SqlDbType = SqlDbType.VarChar;

                p[7] = new SqlParameter("@DMXiMang", double.Parse(txtXiMang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtXiMang.Text.Trim()));
                p[7].SqlDbType = SqlDbType.Float;
                p[8] = new SqlParameter("@DMCat", double.Parse(txtCat.Text.Trim().Replace(",", "").Equals("") ? "0" : txtCat.Text.Trim()));
                p[8].SqlDbType = SqlDbType.Float;
                p[9] = new SqlParameter("@DMDa", double.Parse(txtDa.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa.Text.Trim()));
                p[9].SqlDbType = SqlDbType.Float;
                p[10] = new SqlParameter("@DMNuoc", double.Parse(txtNuoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtNuoc.Text.Trim()));
                p[10].SqlDbType = SqlDbType.Float;
                p[11] = new SqlParameter("@DMPhuGia", double.Parse(txtPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtPhuGia.Text.Trim()));
                p[11].SqlDbType = SqlDbType.Float;

                p[12] = new SqlParameter("@XiMang", double.Parse(txtKLXM.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLXM.Text.Trim()));
                p[12].SqlDbType = SqlDbType.Float;
                p[13] = new SqlParameter("@Cat", double.Parse(txtKLCat.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLCat.Text.Trim()));
                p[13].SqlDbType = SqlDbType.Float;
                p[14] = new SqlParameter("@Da", double.Parse(txtKLDa.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLDa.Text.Trim()));
                p[14].SqlDbType = SqlDbType.Float;
                p[15] = new SqlParameter("@PhuGia", double.Parse(txtKLPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLPhuGia.Text.Trim()));
                p[15].SqlDbType = SqlDbType.Float;


                p[16] = new SqlParameter("@R3", txtR3.Text.Trim());
                p[16].SqlDbType = SqlDbType.VarChar;
                p[17] = new SqlParameter("@R7", txtR7.Text.Trim());
                p[17].SqlDbType = SqlDbType.VarChar;
                p[18] = new SqlParameter("@R28", txtR28.Text.Trim());
                p[18].SqlDbType = SqlDbType.VarChar;


                ex.ExecuteNonQuerry("sp_xdathinghiemtram_Add", p);
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThí nghiệm tại trạm\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            try
            {

                Guid gID = new Guid(hdID.Value);
                p = new SqlParameter[19];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtNgayThang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@Mac", ex.GetGuid(dlMac.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@LoaiDa", int.Parse(dlLoaiDa.SelectedValue));
                p[3].SqlDbType = SqlDbType.Int;
                p[4] = new SqlParameter("@LoaiPhuGia", ex.GetGuid(dlLoaiPhuGia.SelectedValue));
                p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                p[5] = new SqlParameter("@DoSutTK", txtDoSutTK.Text.Trim());
                p[5].SqlDbType = SqlDbType.VarChar;
                p[6] = new SqlParameter("@DoSutTT", txtDoSutTT.Text.Trim());
                p[6].SqlDbType = SqlDbType.VarChar;

                p[7] = new SqlParameter("@DMXiMang", double.Parse(txtXiMang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtXiMang.Text.Trim()));
                p[7].SqlDbType = SqlDbType.Float;
                p[8] = new SqlParameter("@DMCat", double.Parse(txtCat.Text.Trim().Replace(",", "").Equals("") ? "0" : txtCat.Text.Trim()));
                p[8].SqlDbType = SqlDbType.Float;
                p[9] = new SqlParameter("@DMDa", double.Parse(txtDa.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa.Text.Trim()));
                p[9].SqlDbType = SqlDbType.Float;
                p[10] = new SqlParameter("@DMNuoc", double.Parse(txtNuoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtNuoc.Text.Trim()));
                p[10].SqlDbType = SqlDbType.Float;
                p[11] = new SqlParameter("@DMPhuGia", double.Parse(txtPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtPhuGia.Text.Trim()));
                p[11].SqlDbType = SqlDbType.Float;

                p[12] = new SqlParameter("@XiMang", double.Parse(txtKLXM.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLXM.Text.Trim()));
                p[12].SqlDbType = SqlDbType.Float;
                p[13] = new SqlParameter("@Cat", double.Parse(txtKLCat.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLCat.Text.Trim()));
                p[13].SqlDbType = SqlDbType.Float;
                p[14] = new SqlParameter("@Da", double.Parse(txtKLDa.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLDa.Text.Trim()));
                p[14].SqlDbType = SqlDbType.Float;
                p[15] = new SqlParameter("@PhuGia", double.Parse(txtKLPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLPhuGia.Text.Trim()));
                p[15].SqlDbType = SqlDbType.Float;


                p[16] = new SqlParameter("@R3", txtR3.Text.Trim());
                p[16].SqlDbType = SqlDbType.VarChar;
                p[17] = new SqlParameter("@R7", txtR7.Text.Trim());
                p[17].SqlDbType = SqlDbType.VarChar;
                p[18] = new SqlParameter("@R28", txtR28.Text.Trim());
                p[18].SqlDbType = SqlDbType.VarChar;


                ex.ExecuteNonQuerry("sp_xdathinghiemtram_Update", p);
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThí nghiệm tại trạm\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        dlLoaiDa.SelectedIndex = 0;
        dlLoaiPhuGia.SelectedIndex = 0;
        dlMac.SelectedIndex = 0;

        txtNgayThang.Text = "";
        txtXiMang.Text = "";
        txtCat.Text = "";
        txtDa.Text = "";
        txtNuoc.Text = "";
        txtPhuGia.Text = "";

        txtKLCat.Text = "";
        txtKLDa.Text = "";
        txtKLPhuGia.Text = "";
        txtKLXM.Text = "";

        txtDoSutTT.Text = "";
        txtDoSutTK.Text = "";
        
        txtR28.Text = "";
        txtR3.Text = "";
        txtR7.Text = "";

        btnSave.Visible = true;
        btncapnhat.Visible = false;

        btnNext.Visible = true;
        btnPre.Visible = true;
        btnFirst.Visible = true;

        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdathinghiemtram_List", p);
        GV.DataBind();
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@Thang", int.Parse(dlThangLoc.SelectedValue));
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@Nam", int.Parse(dlNamLoc.SelectedValue));
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdathinghiemtram_Search", p);
        GV.DataBind();

        btnNext.Visible = false;
        btnPre.Visible = false;
        btnFirst.Visible = false;
    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {

    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdathinghiemtram_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdathinghiemtram_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdathinghiemtram_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_xdathinghiemtram_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    txtNgayThang.Text = dt.Rows[0]["Ngay"].ToString();
                    dlMac.SelectedValue = dt.Rows[0]["Mac"].ToString();
                    dlLoaiDa.SelectedValue = dt.Rows[0]["LoaiDa"].ToString();
                    dlLoaiPhuGia.SelectedValue = dt.Rows[0]["LoaiPhuGia"].ToString();


                    txtXiMang.Text = dt.Rows[0]["DMXiMang"].ToString();
                    txtCat.Text = dt.Rows[0]["DMCat"].ToString();
                    txtDa.Text = dt.Rows[0]["DMDa"].ToString();
                    txtNuoc.Text = dt.Rows[0]["DMNuoc"].ToString();
                    txtPhuGia.Text = dt.Rows[0]["DMPhuGia"].ToString();

                    txtKLXM.Text = dt.Rows[0]["XiMang"].ToString();
                    txtKLCat.Text = dt.Rows[0]["Cat"].ToString();
                    txtKLDa.Text = dt.Rows[0]["Da"].ToString();
                    txtKLPhuGia.Text = dt.Rows[0]["PhuGia"].ToString();

                    txtDoSutTK.Text = dt.Rows[0]["DoSutTK"].ToString();
                    txtDoSutTT.Text = dt.Rows[0]["DoSutTT"].ToString();

                    
                    txtR3.Text = dt.Rows[0]["R3"].ToString();
                    txtR7.Text = dt.Rows[0]["R7"].ToString();
                    txtR28.Text = dt.Rows[0]["R28"].ToString();

                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThí nghiệm tại trạm\t" + ax.Message);
                }

            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_xdathinghiemtram_Del", p);
                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThí nghiệm tại trạm\t" + ax.Message);
                        gstGetMess(ax.Message, "");
                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
            }
    }
    protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            HeaderGridRow.Height = 20;
            HeaderGridRow.HorizontalAlign = HorizontalAlign.Center;
            TableCell HeaderCell = new TableCell();

            HeaderCell = new TableCell();
            HeaderCell.Text = "&nbsp;";
            HeaderCell.ColumnSpan = 6;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Định mức cấp phối";
            HeaderCell.ColumnSpan = 5;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Thông tin vật liệu";
            HeaderCell.ColumnSpan = 4;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Độ sụt";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);
            

            HeaderCell = new TableCell();
            HeaderCell.Text = "Kết quả";
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);


            GV.Controls[0].Controls.AddAt(0, HeaderGridRow);
        }
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    void LoadPhuGia(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaxuathang_ListLoaiPhuGia");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn phụ gia--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadMac(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdamacbetong_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
}